
package com.ifcar.app.fragment;

import com.ifcar.app.util.Constant;
import com.ifcar.app.util.NeteaseHttpClient;
import com.ifcar.app.util.PicUtil;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Tools;
import com.ifcar.app.util.BaseImageDownloader.FlushedInputStream;
import com.ifcar.app.activity.R;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.preference.PreferenceManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public abstract class NRPullLoadingListWithHeaderFrament extends NRAbsPullLoadingListFrament {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 14:
                ImageView top_image = (ImageView) msg.obj;
                top_image.setImageResource(R.drawable.vote_default);
                // top_image.setImageDrawable(getResources().getDrawable(R.drawable.vote_default));
                break;
            case 15:
                ImageView top_image2 = (ImageView) msg.obj;
                Bundle bundle = msg.getData();
                top_image2.setImageBitmap((Bitmap) bundle.getParcelable("headpic"));
                break;

            default:
                break;
        }
    }

    /**
     * 设置头图
     * @param first
     * @param top_image
     * @param key
     */
    public void setHeaderPic(Map<String, Object> first, final ImageView top_image, String key) {
        final Context context = getActivity();
        
        String imgsrc = "";
        if (top_image == null)
            return;
        if (first.get(key) != null) {
            imgsrc = StringUtil.GetString(first.get(key));
        }
        imgsrc = PicUtil.getHeandBitMapURL(imgsrc, context);
        // Log.i("sethead", imgsrc);
        SharedPreferences set_prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean downpic = set_prefs.getBoolean("downpic", false);
        HashMap<String, Integer> whmap = Tools.getWidth_Height(context);
        int screenwidth = whmap.get("w");
        int w = screenwidth;
        int h = Tools.getCutHeadPicHeight(screenwidth);

        top_image.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, h));
        if (imgsrc != null && !imgsrc.equals("")) {
            String localImgUrl = imgsrc.substring(imgsrc.lastIndexOf("/") + 1, imgsrc.length());
            if (PicUtil.isLocalPicExit(localImgUrl)) {
                Bitmap bitmap = PicUtil.readTempJPEGFile(localImgUrl);
                if (null != bitmap) {
                    top_image.setImageBitmap(bitmap);
                } else {
                    top_image.setImageResource(R.drawable.vote_default);
                }

            } else {

                top_image.setImageResource(R.drawable.vote_default);

                if (Tools.isshowimg(downpic, context)) {
                    final String url = imgsrc;
                    final int bitmapW = w;
                    final int bitmapH = h;

                    new Thread() {
                        public void run() {
                            Bitmap bm;
                            Message msg = new Message();
                            msg.obj = top_image;
                            // Log.i("setHeadpic", url);
                            try {
                                if (url == null || url.equals("")) {
                                    msg.what = 14;
                                    getHandler().sendMessage(msg);

                                } else {
                                    HttpParams httpParams = new BasicHttpParams();
                                    // 设置连接超时和数据读取超时
                                    HttpConnectionParams.setConnectionTimeout(httpParams,
                                            Constant.TIMEOUT);
                                    HttpConnectionParams.setSoTimeout(httpParams, Constant.TIMEOUT);
                                    DefaultHttpClient httpClient = NeteaseHttpClient
                                            .getHttpClient(httpParams);
                                    
                                    Tools.setHttpClientHostProxy(httpClient, context);

                                    HttpGet request = new HttpGet(PicUtil.getImgUrlByNetType(url,context));
                                    HttpResponse response = httpClient.execute(request);
                                    HttpEntity entity = response.getEntity();
                                    InputStream is = entity.getContent();

                                    if (is == null) {
                                        msg.what = 14;
                                        getHandler().sendMessage(msg);
                                    } else {
                                        bm = PicUtil.cutBitmap(BitmapFactory
                                                .decodeStream(new FlushedInputStream(is)), bitmapW,
                                                bitmapH);
                                        if (null != bm) {
                                            PicUtil.saveMyBitmap(url.substring(
                                                    url.lastIndexOf("/") + 1, url.length()), bm);
                                            msg.what = 15;
                                            Bundle bundle = new Bundle();
                                            bundle.putParcelable("headpic", bm);
                                            msg.setData(bundle);
                                            getHandler().sendMessage(msg);
                                        } else {
                                            msg.what = 14;
                                            getHandler().sendMessage(msg);
                                        }

                                        is.close();
                                        entity.consumeContent();
                                    }

                                }
                            } catch (Exception e) {

                                msg.what = 14;
                                getHandler().sendMessage(msg);

                            } catch (OutOfMemoryError e) {
                                msg.what = 14;
                                getHandler().sendMessage(msg);
                            }

                        }
                    }.start();
                }

            }
        } else {

            top_image.setImageResource(R.drawable.vote_default);
            // top_image.setImageDrawable(getResources().getDrawable(R.drawable.vote_default));
        }
    }
}
